package recursivecode;

import java.util.ArrayList;
import java.util.List;

/**
 * 功能描述:字符全排列
 * 2024/07/12
 *
 * @author ZhangYi
 */
public class FullPermutationOfCharacters {
	public static void main(String[] args) {
		String str = "abca";
		System.out.println(getPermutation(str));
	}

	private static List<String> getPermutation(String str) {
		if (str == null || str.length() == 0){
			return new ArrayList<>();
		}

		List<String> result = new ArrayList<>();
		char[] chars = str.toCharArray();
		parseAllPermutation(chars, 0,"", result);
		return result;
	}

	private static void parseAllPermutation(char[] chars, int index, String path, List<String> result) {
		if (index == chars.length && !result.contains(path)){
			result.add(path);
		}

		for (int i = index; i < chars.length; i++) {
			for (int j = i; j < chars.length; j++) {
				swap(chars, i, j);
				parseAllPermutation(chars, index + 1, path +chars[index] , result);
				swap(chars, i, j);
			}
		}
	}

	private static void swap(char[] chars, int i, int j) {
		char temp = chars[i];
		chars[i] = chars[j];
		chars[j] = temp;
	}
}
